package com.android.baseconfig.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.android.baseconfig.common.helper.ActivityManager;
import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.ToastUtils;

import java.util.List;
import java.util.Stack;


/**
 * @author lei
 * @description BaseConfigApplication
 * @date 2018/5/5
 */
public class BaseConfigApplication extends Application implements BaseHandler.BaseHandlerCallBack {
    public static final String TAG = "";
    protected static BaseConfigApplication mInstance;
    public BaseHandler<BaseHandler.BaseHandlerCallBack> mainHandler;
    private int count = 0;
    private Activity currentActivity;
    private Activity pauseActivity;

    public static BaseConfigApplication getInstance() {
        return mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;

        // Logc.i(TAG, "onCreate");
        mainHandler = new BaseHandler<BaseHandler.BaseHandlerCallBack>(this);
        // CrashHandler crashHandler = CrashHandler.getInstance();
        // crashHandler.init(getApplicationContext());

        // initOKhttp();

        registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

            @Override
            public void onActivityStopped(Activity activity) {
                Logc.v(TAG, activity.getClass().getSimpleName() + " --> onActivityStopped");
                count--;
                if (count == 0) {
                    // Logc.v(TAG, ">>>>>>>>>>>>>>>>>>>切到后台  lifecycle");
                }
            }

            @Override
            public void onActivityStarted(Activity activity) {
                // Logc.v(TAG, activity.getClass().getSimpleName() + " --> onActivityStarted");
                count++;
                if (count == 1) {
                    // Logc.v(TAG, ">>>>>>>>>>>>>>>>>>>切到前台  lifecycle");
                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
                // Log.v(TAG, activity.getClass().getSimpleName() + " --> onActivitySaveInstanceState");
            }

            @Override
            public void onActivityResumed(Activity activity) {
                Logc.v(TAG, activity.getClass().getSimpleName() + " --> onActivityResumed");
                setCurrentActivity(activity);
                ActivityManager.ForegroundActivity = activity;
                RxBus3.getInstance().post("ACTIVITY_RESUMED", activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {
                Logc.v(TAG, activity.getClass().getSimpleName() + " --> onActivityPaused");
                setPauseActivity(activity);
                ActivityManager.ForegroundActivity = null;
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                Logc.v(TAG, activity.getClass().getSimpleName() + " --> onActivityDestroyed");
                ActivityManager.removeActivity(activity);
                RxBus3.getInstance().post("ACTIVITY_DESTROYED", activity);
            }

            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                Logc.v(TAG, activity.getClass().getSimpleName() + " --> onActivityCreated");
                ActivityManager.addActivity(activity);
                RxBus3.getInstance().post("ACTIVITY_CREATED", activity);
            }
        });
    }

    public Handler getMainHandler() {
        return mainHandler;
    }

    protected void initOKhttp() {

    }

    public static Context getBaseApplication() {
        return mInstance;
    }

    public boolean isSwitchBackground() {
        return count == 0;
    }

    public boolean isSwitchForeground() {
        return count == 1;
    }

    // 遍历所有Activity并finish
    public void exit() {
        Stack<Activity> activities = ActivityManager.getAllActivity();
        for (Activity activity : activities) {
            activity.finish();
        }

        System.exit(0);
    }

    public void finishWithoutActivity(Class<?> cls) {
        Stack<Activity> activities = ActivityManager.getAllActivity();
        for (Activity activity : activities) {
            if (!(activity.getClass().getName().equals(cls.getName()))) {
                activity.finish();
            }
        }
    }

    public void finishActivity(Class<?> cls) {
        Stack<Activity> activities = ActivityManager.getAllActivity();
        for (Activity activity : activities) {
            if (activity.getClass().getName().equals(cls.getName())) {
                activity.finish();
            }
        }
    }

    public void setCurrentActivity(Activity currentActivity) {
        this.currentActivity = currentActivity;
    }

    public Activity getCurrentActivity() {
        return currentActivity;
    }

    public Activity getPauseActivity() {
        return pauseActivity;
    }

    public void setPauseActivity(Activity pauseActivity) {
        this.pauseActivity = pauseActivity;
    }

    /**
     * 判断APP是否在后台运行 <br>
     *
     * @return true=后台运行
     */
    public boolean isRunningBackground() {
        android.app.ActivityManager manager = (android.app.ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

        List<android.app.ActivityManager.RunningAppProcessInfo> appProcessInfo = manager.getRunningAppProcesses();
        if (null == appProcessInfo) {
            return false;
        }

        String packageName = getPackageName();

        for (android.app.ActivityManager.RunningAppProcessInfo appInfo : appProcessInfo) {
            if (packageName.equals(appInfo.processName)) {
                if (appInfo.importance == android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    return false;
                }

                return true;
            }
        }

        return false;
    }

    @Override
    public void callBack(Message msg) {
        ToastUtils.getInstance().showToast(this, msg.obj.toString());
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

}
